-- Add all of the clientside LUA files
AddCSLuaFile("autorun/sledge.lua")
AddCSLuaFile("sledge_client.lua")

sledge = {}
sledge.Release = false
sledge.Messages = 0

-- Get the current version
function sledge.GetVersion()
	if(file.Exists("../lua/sledgeplugs/.svn/entries")) then
		ver = "SVN revision "..tonumber(string.explode("\n", file.Read("../lua/sledgeplugs/.svn/entries"))[4])
	elseif(sledge.Release == false and !file.Exists("../lua/sledgeplugs/.svn/entries")) then
		ver = "SVN revision <unknown>"
	elseif(sledge.Release == true) then
		ver = "Release "..sledge.Version
	else
		ver = "How did you get here?"
	end
	return ver
end

-- Get a player by many different methods
function sledge.findplayer(user)
	for k, v in pairs(player.GetAll()) do
		if(tonumber(user) == v:UserID() or user:lower() == v:SteamID():lower() or string.find(v:Nick():lower(), user:lower()) or user:lower() == string.explode(":", v:IPAddress():lower())[1]) then
			return v
		end
	end
	return false
end

-- Send a message to someone
function sledge.Msg(ply, msg)
	if(ply and ply:IsValid()) then
		ply:PrintMessage(HUD_PRINTTALK, tostring(msg))
		sledge.Messages = sledge.Messages+1
		return true
	else
		return false
	end
end

-- Sends a message to all
function sledge.MsgAll(msg)
	for k, v in pairs(player.GetAll()) do
		if(v and v:IsValid()) then
			v:PrintMessage(HUD_PRINTTALK, tostring(msg))
			sledge.Messages = sledge.Messages+1
		end
	end
	return true
end

-- Send debug message
function sledge.Debug(msg)
	Msg("Sledge -> "..msg.."\n")
end

-- Make the gum command system function
function sledge_cmd(ply, cmd, args)
	noaccess = false
	if(args[1] == nil) then
		sledge.Msg(ply, "Usage: sledge <cmd> <args>")
		return nil
	end
	if(args[1] == "plugins") then
		if(args[2] == "list") then
			local pluglist = sledge.GetPlugs()
			if(#pluglist <= 0) then
				sledge.Msg(ply, "No plugins loaded")
			end
			for k,v in pairs(pluglist) do
				sledge.Msg(ply, v.Name.." by "..v.Author.." - "..v.File)
			end
		elseif(args[2] == "load") then
			sledge.Msg(ply, "Will be implemented in a later version")
		elseif(args[2] == "reload") then
			sledge.Msg(ply, "Reloading plugins...")
			sledge.FindPlugs(nil)
			sledge.Msg(ply, "Done reloading plugins.")
		elseif(args[2] == "unload") then
			sledge.Msg(ply, "Will be implemented in a later version")
		else
			sledge.Msg(ply, "Unknown command "..args[2]..", valid commands are: load, reload, unload, list")
		end
	elseif(args[1] == "version") then
		sledge.Msg(ply, "Version: "..sledge.GetVersion())
		sledge.Msg(ply, "Sent "..sledge.Messages.." messages since loading.")
	else
		local called = 0
		for k,v in pairs(sledge.plugs) do
			for k2,v2 in pairs(v.Commands) do
				if(v2 == args[1]) then
					v.CallCommand(ply, cmd, args)
					called = 1
				end
			end
		end
		if(called == 0) then
			sledge.Msg(ply, "No such command \""..args[1].."\"")
		end
	end
	if(noaccess) then
		SendMessage(ply, "You do not have access to this command")
	end
end
-- Create the gum command system
concommand.Add("sledge", sledge_cmd)
